﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AutoItX3Lib;

namespace Pokerist
{
    public partial class PlayMultiplePoker : Form
    {
        AutoItX3 autoit = new AutoItX3();

        struct Poker
        {
            public int PokerID;
            public int callAnyPosX;
            public int callAnyPosY;
            public int checkPosX;
            public int checkPosY;
            public string winTitle;
        }

        List<Poker> PokerPlayingList = new List<Poker>();
        int currentPoker = 0;
        
        public PlayMultiplePoker()
        {
            InitializeComponent();

            InitForm();
        }
                
        public bool InitForm()
        {
            string pokerName, email, winTitle;

            for (int i = 1; i < 27; i++) {
                pokerName = string.Format("justforme{0}z", Convert.ToChar(96 + i));
                email = string.Format("{0}@pk.pk", pokerName);
                winTitle = string.Format("Pokerist robot - {0}", email);

                grdMain.Rows.Add(i, "Go", pokerName, email, "zxc", 502, 559, 398, 559, winTitle);
            }

            return true;
        }
        
        private void grdMain_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex != 1) return;

            if (grdMain[e.ColumnIndex, e.RowIndex].Value.ToString() != "Playing")
            {
                grdMain[e.ColumnIndex, e.RowIndex].Value = "Playing";
                grdMain[e.ColumnIndex, e.RowIndex].Style.Font = new Font(grdMain.Font, FontStyle.Bold);

                PokerPlayingList.Add(InitPoker(e.RowIndex));
            }
            else
            {
                grdMain[e.ColumnIndex, e.RowIndex].Value = "Pause";
                grdMain[e.ColumnIndex, e.RowIndex].Style.Font = new Font(grdMain.Font, FontStyle.Italic);

                PokerPlayingList.Remove(InitPoker(e.RowIndex));
            }
        }

        private Poker InitPoker(int rowID)
        {
            return new Poker
            {
                PokerID = Convert.ToInt32(grdMain["SN", rowID].Value),
                callAnyPosX = Convert.ToInt32(grdMain["CallAnyPosX", rowID].Value),
                callAnyPosY = Convert.ToInt32(grdMain["CallAnyPosY", rowID].Value),
                checkPosX = Convert.ToInt32(grdMain["CheckPosX", rowID].Value),
                checkPosY = Convert.ToInt32(grdMain["CheckPosY", rowID].Value),
                winTitle = grdMain["WindowTitle", rowID].Value.ToString()
            };
        }

        private void PokerScan_Tick(object sender, EventArgs e)
        {
            if (PokerPlayingList.Count == 0) return;
            if (currentPoker >= PokerPlayingList.Count) currentPoker = 0;

            //click call any button
            autoit.ControlClick(PokerPlayingList[currentPoker].winTitle, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", 1,
                PokerPlayingList[currentPoker].callAnyPosX, PokerPlayingList[currentPoker].callAnyPosY);

            //click check button
            autoit.ControlClick(PokerPlayingList[currentPoker].winTitle, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", 1,
                PokerPlayingList[currentPoker].checkPosX, PokerPlayingList[currentPoker].checkPosY);

            currentPoker++;            
        }
    }
}
